package Page.LoginAndExitRecordPage;

import Navigation.NavigationController;
import DataClass.LoginAndExit;
import Util.TimeUtil;
import javafx.beans.property.*;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yooo_fan
 */
public class InAndOutViewModel {
    ListProperty<LoginAndExit> dataList = new SimpleListProperty<>(FXCollections.observableArrayList());
    // 保存数据副本
    private List<LoginAndExit> loginAndExitCopy = new ArrayList<LoginAndExit>();

    private final StringProperty inputId = new SimpleStringProperty();

    private final ObjectProperty<LocalDate> inputDate = new SimpleObjectProperty<>();

    public StringProperty getInputId() {
        return inputId;
    }
    public void updateInputId(String inputName) {
        this.inputId.set(inputName);
    }

    public ObjectProperty<LocalDate> getInputDate() {
        return inputDate;
    }
    public void updateInputDate(LocalDate inputDate) {
        this.inputDate.set(inputDate);
    }

    public void backButtonClicked(){
        NavigationController.back();
    }

    public void setDataList(ObservableList<LoginAndExit> dataList) {
        this.dataList.setAll(dataList);
    }

    public ListProperty<LoginAndExit> getDataListProperty() {
        return dataList;
    }

    public void setDataList(List<LoginAndExit> dataList) {
        this.loginAndExitCopy = dataList;
        setDataList(FXCollections.observableArrayList(loginAndExitCopy));
    }

    /**
     * 刷新数据
     */
    public void refresh() {
        this.inputId.set(null);
        this.inputDate.set(null);
    }

    /**
     * 过滤列表
     */
    public void findButtonClicked() {
        System.out.println(inputId.get());
        System.out.println(inputDate.get());

        String targetId = inputId.get();
        LocalDate targetDate = inputDate.get();

        if(targetId != null && targetDate != null && !targetId.isEmpty()) {
            System.out.println("通过时间和id查询");
            FilteredList<LoginAndExit> filteredData = new FilteredList<>(
                    FXCollections.observableArrayList(loginAndExitCopy),
                    p -> (Long.parseLong(p.getRUid()) == Long.parseLong(targetId)&&
                            (TimeUtil.getIfSame(p.getInfoInTime(),targetDate) ||
                                    TimeUtil.getIfSame(p.getInfoOutTime(),targetDate)))
            );
            dataList.setAll(filteredData);
            return;
        }
        if(targetId != null && !targetId.isEmpty()){
            System.out.println("通过id查询"+inputId.get());
            FilteredList<LoginAndExit> filteredData = new FilteredList<>(
                    FXCollections.observableArrayList(loginAndExitCopy),
                    p->(Long.parseLong(p.getRUid()) == Long.parseLong(targetId))
            );
            dataList.setAll(filteredData);
            return;
        }
        if(targetDate != null){
            System.out.println("通过时间查询");
            FilteredList<LoginAndExit> filteredData = new FilteredList<>(
                    FXCollections.observableArrayList(loginAndExitCopy),
                    p -> (TimeUtil.getIfSame(p.getInfoInTime(),targetDate) ||
                            TimeUtil.getIfSame(p.getInfoOutTime(),targetDate))
            );
            dataList.setAll(filteredData);
        }
    }

    /**
     * 清空过滤
     */
    public void clearFilter() {
        this.inputId.set(null);
        this.inputDate.set(null);
        this.setDataList(loginAndExitCopy);
    }
}
